class HubWidgetsController < ApplicationController
  before_filter :find_hub
  
  # GET /hubs
  # GET /hubs.xml
  def index
    @hub_widgets = @hub.hub_widgets.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @hub_widgets }
    end
  end
  
  # GET /hubs/new
  # GET /hubs/new.xml
  def new
    @hub_widget = @hub.hub_widgets.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @hub_widget }
    end
  end

  # GET /hubs/1/edit
  def edit
    @hub_widget = @hub.hub_widgets.find(params[:id])
  end
  

  # POST /hubs
  # POST /hubs.xml
  def create
    @hub_widget = @hub.hub_widgets.build(params[:hub_widget])

    respond_to do |format|
      if @hub_widget.save
        flash[:notice] = 'Widget was successfully associated with the hub.'
        format.html { redirect_to(@hub) }
        format.xml  { render :xml => @hub_widget, :status => :created, :location => @hub }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @hub_widget.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /hubs/1
  # PUT /hubs/1.xml
  def update
    @hub_widget = @hub.hub_widgets.find(params[:id])

    respond_to do |format|
      if @hub_widget.update_attributes(params[:hub_widget])
        flash[:notice] = 'Widget association was successfully updated.'
        format.html { redirect_to(hub_hub_widgets_path(@hub)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @hub_widget.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /hubs/1
  # DELETE /hubs/1.xml
  def destroy
    @hub_widget = @hub.hub_widgets.find(params[:id])
    @hub_widget.destroy

    respond_to do |format|
      format.html { redirect_to(@hub) }
      format.xml  { head :ok }
    end
  end
  
  protected
  
  def find_hub
    @hub = Hub.find(params[:hub_id])
  end
end
